package com.applet.applet_shangcheng.controller;

import com.applet.applet_shangcheng.entity.CashDiscount;
import com.applet.applet_shangcheng.service.CashDiscountService;
import com.applet.applet_shangcheng.utlis.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cashDiscount")
@CrossOrigin
@Api(value="兑换码管理",tags={"兑换码管理"})
public class CashDiscountController {

    @Autowired
    private CashDiscountService cashDiscountService;

    /*
   参数:
     */
    @ApiOperation(value="添加兑换码")
    @PostMapping("/insert")
    public Result insertCashDiscount(@RequestBody CashDiscount cashDiscount){
        if (cashDiscount.gettCode()==null){
            return new Result(false, StatusCode.ERROR,"请输入兑换码!");
        }
       if (cashDiscount.getIntegral()==0&&cashDiscount.getMoney()==0&&cashDiscount.getBalance()==null){
            return new Result(false, StatusCode.ERROR,"您还没有选择奖励!");
        }
        if (cashDiscount.gettCode()!=null){
            CashDiscount cashDiscount1 = cashDiscountService.selectCashDiscountByCode(cashDiscount.gettCode());
            if (cashDiscount1!=null){
                return new Result(false, StatusCode.ERROR,"兑换码已存在!");
            }
            int result = cashDiscountService.insertCashDiscount(cashDiscount);
            if (result>0){
                return new Result(true, StatusCode.OK,"添加兑换码优惠成功!");
            }
            return new Result(false, StatusCode.ERROR,"添加兑换码优惠失败!");
        }
        int result = cashDiscountService.insertCashDiscount(cashDiscount);
        if (result>0){
            return new Result(true, StatusCode.OK,"添加兑换码优惠成功!");
        }
        return new Result(false, StatusCode.ERROR,"添加兑换码优惠失败!");
    };
    //兑换奖励
    @ApiOperation(value="兑换积分")
    @GetMapping("/exchange/{tCode}/{userId}")
    public Result updateCashDiscount(@PathVariable("code") String tCode,
                                     @PathVariable("userId") int userId){
        CashDiscount cashDiscount = cashDiscountService.selectCashDiscountByCode(tCode);
        if (cashDiscount==null){
            return new Result(false, StatusCode.ERROR,"兑换码不正确!");
        }
        if (cashDiscount.getStatus()==0){
            return new Result(false, StatusCode.ERROR,"兑换码已失效!");
        }
        if (tCode.equalsIgnoreCase(cashDiscount.gettCode())){

            Map map = cashDiscountService.updateCashDiscount(tCode, userId);
            if (map!=null){
                return new Result(true, StatusCode.OK,"修改兑换码状态成功!",map);
            }
            return new Result(false, StatusCode.ERROR,"修改兑换码状态失败!");
        }
        return new Result(false, StatusCode.ERROR,"兑换码不正确!");
    };
    @ApiOperation(value="查询所有兑换码")
    @GetMapping("/findByUserId")
    public  Result selectCashDiscountByUserId(@RequestParam("userId") String userId){
        List<CashDiscount> cashDiscount = cashDiscountService.selectCashDiscountByUserId(userId);
        return new Result(true, StatusCode.OK,"查询兑换码状态成功!",cashDiscount);
    };
    @ApiOperation(value="查询兑换码状态")
    @GetMapping("/findById/{id}")
    public  Result findById(@PathVariable("id") int id){
        CashDiscount cashDiscount = cashDiscountService.findById(id);
        return new Result(true, StatusCode.OK,"查询兑换码状态成功!",cashDiscount);
    };

    @ApiOperation(value="修改兑换码状态")
    @GetMapping("/findByCode")
    public  Result selectCashDiscountByCode(@RequestParam("code") String code){
        CashDiscount cashDiscount = cashDiscountService.selectCashDiscountByCode(code);
        return new Result(true, StatusCode.OK,"修改兑换码状态成功!",cashDiscount);
    };

    @ApiOperation(value="删除兑换码")
    @GetMapping("/deleteCashDiscountByCode/{code}")
    public  Result deleteCashDiscountByCode(@PathVariable("code") String code){
        int count = cashDiscountService.deleteCashDiscountByCode(code);
        if (count>0){
            return new Result(true, StatusCode.OK,"删除成功!");
        }
        return new Result(false, StatusCode.ERROR,"删除失败!");
    };
    //查询所有
    @ApiOperation(value="查询所有兑换码")
    @GetMapping("/findAll")
    public  Result selectCashDiscountByCode(){
        List<CashDiscount> cashDiscount = cashDiscountService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",cashDiscount);
    };
    @ApiOperation(value="获取随机兑换码")
    @GetMapping("/getCode")
    public  String getCode(){
        String code = AppletUtil.getRandomStringByLength(18);
        return code;
    };
    @ApiOperation(value="_分页查询兑换码")
    @PostMapping("/findAll1")
    public PageResult findAll1(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = cashDiscountService.findAll1(queryPageBean.getQueryString(),queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        return pageResult;
    }
    @ApiOperation(value="修改兑换码")
    @PostMapping("/updateCashDiscountByCode")
    public  Result updateCashDiscountByCode(@RequestBody CashDiscount cashDiscount){
        int count = cashDiscountService.updateCashDiscountByCode(cashDiscount);
        if (count>0){
            return new Result(true, StatusCode.OK,"修改成功",cashDiscount);
        }
        return new Result(false, StatusCode.ERROR,"修改失败!");
    };

}
